package com.wankuma.hcm.midlet.metwit;

class UrlEncode {
	public static String encode(byte[] bin) {
		String buffer = "";
		for (int loop = 0; loop < bin.length; loop++) {
			byte convData = bin[loop];

			if (convData == ' ') {
				buffer = buffer + "+";
			} else if (convData == '\n') {
				buffer = buffer + "%0D%0A";
			} else if (convData == '.') {
				buffer = buffer + ".";
			} else if (convData == '_') {
				buffer = buffer + "_";
			} else if (convData == '-') {
				buffer = buffer + "-";
			} else if (convData == '*') {
				buffer = buffer + "*";
			} else if ((convData >= '0') && (convData <= '9')) {
				int targetData = convData - '0';
				buffer = buffer + targetData;
			} else if ((convData >= 'A') && (convData <= 'Z')) {
				String targetData = new String(bin, loop, 1);
				buffer = buffer + targetData;
				targetData = null;
			} else if ((convData >= 'a') && (convData <= 'z')) {
				String targetData = new String(bin, loop, 1);
				buffer = buffer + targetData;
				targetData = null;
			} else {
				int targetInt = convData;
				if (targetInt < 0) {
					targetInt = targetInt + 256;
				}
				if ((targetInt != 0) && (targetInt != 0x0d)) {
					if (targetInt < 16) {
						buffer = buffer
								+ "%0"
								+ (Integer.toHexString(targetInt))
										.toUpperCase();
					} else {
						buffer = buffer
								+ "%"
								+ (Integer.toHexString(targetInt))
										.toUpperCase();
					}
				}
			}
		}
		return (buffer);
	}

	public static String encode(String str) {
		return encode(BinaryUtil.stringToUTFBytes(str));
	}
}